package com.huawei.lynnbit.tool.cocos.config.game.task;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.Objects;

@Slf4j
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CmdTask implements Task {
    private Stage stage;

    private String type = Task.TYPE_CMD;

    private String runDir;

    private String runCmd;

    @Override
    public String getTaskInfo() {
        return "run cmd: " + runCmd + " in " + runDir;
    }

    @Override
    public boolean isValid() {
        if (Objects.isNull(stage)) {
            log.error("type:{}, stage cannot be blank", type);
            return false;
        }

        if (StringUtils.isNotBlank(runDir)) {
            File file = new File(runDir);
            if (!file.exists()) {
                log.error("type:{}, runDir is not exist", type);
                return false;
            }
        }

        if (StringUtils.isBlank(runCmd)) {
            log.error("type:{}, runCmd cannot be blank", type);
            return false;
        }

        return true;
    }
}
